package com.ysd.lis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.PubTaskPage;
import com.ysd.lis.request.PubTaskPageAddParam;
import com.ysd.lis.util.ListToTreeUtil;
import com.ysd.lis.mapper.PubTaskPageMapper;
import com.ysd.lis.service.PubTaskPageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 样品可配置项分类表 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2023-11-13
 */
@Service
public class PubTaskPageServiceImpl extends ServiceImpl<PubTaskPageMapper, PubTaskPage> implements PubTaskPageService {

    @Resource
    private PubTaskPageMapper pubTaskPageMapper;

    public Result getTaskPageListTree() {
        LambdaQueryWrapper<PubTaskPage> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(PubTaskPage::getDelFlag, "0");
        List<PubTaskPage> pubTaskPages = pubTaskPageMapper.selectList(lambdaQueryWrapper);
        JSONArray objects = ListToTreeUtil.forObjectToTree(pubTaskPages, "0", "id", "parentId", "children");
        return Result.succ(1, "查询成功", objects);
    }

    public Result addTaskPage(PubTaskPageAddParam pubTaskPageAddParam) {
        PubTaskPage pubTaskPage = new PubTaskPage();
        BeanUtil.copyProperties(pubTaskPageAddParam, pubTaskPage);
        boolean save = this.save(pubTaskPage);
        if (save) {
            return Result.succ(1, "添加成功！", save);
        } else {
            return Result.fail("添加失败！");
        }
    }

    public Result editTaskPage(PubTaskPageAddParam pubTaskPageAddParam) {
        PubTaskPage pubTaskPage = new PubTaskPage();
        BeanUtil.copyProperties(pubTaskPageAddParam, pubTaskPage);
        boolean update = this.updateById(pubTaskPage);
        if (update) {
            return Result.succ(1, "修改成功！", update);
        } else {
            return Result.fail("修改失败！");
        }
    }

    public Result deleteTaskPage(String id) {
        LambdaUpdateWrapper<PubTaskPage> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(PubTaskPage::getDelFlag, 1).eq(PubTaskPage::getId, id);
        pubTaskPageMapper.update(null, lambdaUpdateWrapper);
        return Result.succ(1, "删除成功！", null);
    }

}
